﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BarberShop.Model;
using BarberShop.Data;
using System.Linq.Expressions;
using BarberShop.Common;
using System.Xml;
using BarberShop.Common.Enum;
using System.IO;

namespace BarberShop.BLL
{
    public class StoresInfoBLL
    {

        public static IList<StoresInfoModel> GetStoresList(StoresInfoSearch search)
        { 
            using(DataClassesDataContext data = new DataClassesDataContext())
            {
                Expression<Func<StoresInfo, bool>> WhereExpr = PredicateExtensions.True<StoresInfo>();
                if (!string.IsNullOrEmpty(search.StoreName))
                {
                    WhereExpr = WhereExpr.And(x => x.StoreName.Contains(search.StoreName));
                }
                if (search.BrandId!=-1)
                {
                    WhereExpr = WhereExpr.And(x => x.Brand_ID.Equals(search.BrandId));
                }
                if (search.ProvinceId != -1)
                {
                    WhereExpr = WhereExpr.And(x => x.Province_ID.Equals(search.ProvinceId));
                }
                if (search.CityId != -1)
                {
                    WhereExpr = WhereExpr.And(x => x.City_ID.Equals(search.CityId));
                }
                var Stores = data.StoresInfo.Where(WhereExpr);
                return StoresInfoModel.ConvertDataModelConllection(Stores.ToList());
            }
        }
        /// <summary>
        /// 添加门店
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool AddStoresInfo(StoresInfo info)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    data.StoresInfo.InsertOnSubmit(info);
                    data.SubmitChanges();
                    return true;
                }
                catch { return false; }
            }

        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static StoresInfo GetStoresInfo(int id)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var info = (from a in data.StoresInfo where a.ID == id select a).FirstOrDefault();
                return info;
            }
        }

        public static bool UpdateSaveInfo(StoresInfo info)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    var r = (from a in data.StoresInfo where a.ID == info.ID select a).FirstOrDefault();
                    r.Address = info.Address;
                    r.Brand_ID = info.Brand_ID;
                    r.City_ID = info.City_ID;
                    r.ContactBeginDate = info.ContactBeginDate;
                    r.ContactDeadline = info.ContactDeadline;
                    r.DeviceNum = info.DeviceNum;
                    r.ModifyDate = info.ModifyDate;
                    r.Province_ID = info.Province_ID;
                    r.StoreName = info.StoreName;
                    r.TelName = info.TelName;
                    r.TelPhone = info.TelPhone;
                    r.MoviePlay_ID = info.MoviePlay_ID;
                    r.AdImgPlay_ID = info.AdImgPlay_ID;
                    data.SubmitChanges();
                    return true;
                }
                catch { return false; }
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool DelStoresInfo(int id)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var info = (from a in data.StoresInfo where a.ID == id select a).FirstOrDefault();
                if (info != null)
                {
                    data.StoresInfo.DeleteOnSubmit(info);
                    data.SubmitChanges();
                    return true;
                }
                return false;
            }
        }
        /// <summary>
        /// 设置门店数据跟新状态与时间
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <param name="createdate"></param>
        /// <param name="updatedate"></param>
        public static void SetStatusDateTime(int id, FileStatus status, DateTime? createdate, DateTime? updatedate)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    var r = (from a in data.StoresInfo where a.ID == id select a).FirstOrDefault();
                    if (r != null)
                    {
                        r.VerUpdateStatus = (int)status;
                        r.VerCreateDate = createdate;
                        r.VerUpdateDate = updatedate;
                    }
                    data.SubmitChanges();
                }
                catch {  }
            }
        }
        /// <summary>
        /// 生成门店XML
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static void CreateShopXML(int id)
        {
            try
            {
                string UpFileUrl = Maticsoft.Common.ConfigHelper.GetConfigString("UpFileUrl");
                string shoppath = UpFileUrl + "Shop_" + id + "\\Shop.xml";
                string strxml = string.Empty;
                XmlDocument xmldoc = new XmlDocument();
                XmlNode Xnode = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", "");
                xmldoc.AppendChild(Xnode);
                XmlElement root = xmldoc.CreateElement("Root");
                xmldoc.AppendChild(root);
                #region barber
                //barber begin
                XmlElement BarberElm = xmldoc.CreateElement("barber");
                XmlElement TagsElm = xmldoc.CreateElement("tags");
                XmlElement ItemsElm = xmldoc.CreateElement("items");
                var tagTypelist = BarberTypeBLL.GetBarberTypeList("");
                foreach (var item in tagTypelist)
                {
                    XmlElement tagitem = xmldoc.CreateElement("tag");
                    tagitem.SetAttribute("name", item.Name);
                    tagitem.SetAttribute("key", item.ID.ToString());
                    TagsElm.AppendChild(tagitem);
                }
                var tagItemlist = BarberInfoBLL.GetBarberList(new BarberInfoSearch { StoreId = id });
                foreach (var item in tagItemlist)
                {
                    XmlElement tagitem = xmldoc.CreateElement("item");
                    tagitem.SetAttribute("id", item.ID.ToString());
                    tagitem.SetAttribute("name", item.Name);
                    tagitem.SetAttribute("img", item.Bigimgname);
                    tagitem.SetAttribute("midimg", item.Bigimgname);
                    tagitem.SetAttribute("snapimg", item.Smallimgname);
                    tagitem.SetAttribute("desc", item.Description);
                    tagitem.SetAttribute("tag", item.Type.ToString());
                    tagitem.SetAttribute("hair", "");
                    ItemsElm.AppendChild(tagitem);
                }

                BarberElm.AppendChild(TagsElm);
                BarberElm.AppendChild(ItemsElm);
                #endregion

                #region Hair

                //Hair begin
                XmlElement HairElm = xmldoc.CreateElement("hair");
                XmlElement HairTagsElm = xmldoc.CreateElement("tags");
                XmlElement HairItemsElm = xmldoc.CreateElement("items");
                var HairtagTypelist = HairstyleTypeBLL.GetHairstyleList("");
                foreach (var item in HairtagTypelist)
                {
                    XmlElement tagitem = xmldoc.CreateElement("tag");
                    tagitem.SetAttribute("name", item.Name);
                    tagitem.SetAttribute("key", item.ID.ToString());
                    HairTagsElm.AppendChild(tagitem);
                }
                var HairItemlist = HairstyleInfoBLL.GetHairstyleList(new HairstyleInfoSearch { StoreId = id });
                foreach (var item in HairItemlist)
                {
                    XmlElement tagitem = xmldoc.CreateElement("item");
                    tagitem.SetAttribute("id", item.ID.ToString());
                    tagitem.SetAttribute("name", item.Name);
                    tagitem.SetAttribute("img", item.Bigimgname);
                    tagitem.SetAttribute("midimg", item.Bigimgname);
                    tagitem.SetAttribute("snapimg", item.Smallimgname);
                    tagitem.SetAttribute("desc", item.Description);
                    tagitem.SetAttribute("tag", item.Type.ToString());
                    tagitem.SetAttribute("barber", "");
                    tagitem.SetAttribute("hairdye", "");
                    HairItemsElm.AppendChild(tagitem);
                }

                HairElm.AppendChild(HairTagsElm);
                HairElm.AppendChild(HairItemsElm);

                #endregion

                #region product
                //product begin
                XmlElement ProductElm = xmldoc.CreateElement("product");
                XmlElement ProductTagsElm = xmldoc.CreateElement("tags");
                XmlElement ProductItemsElm = xmldoc.CreateElement("items");
                var ProducttagTypelist = ProductTypeBLL.GetProductList("");
                foreach (var item in ProducttagTypelist)
                {
                    XmlElement tagitem = xmldoc.CreateElement("tag");
                    tagitem.SetAttribute("name", item.Name);
                    tagitem.SetAttribute("key", item.ID.ToString());
                    ProductTagsElm.AppendChild(tagitem);
                }
                var ProductItemlist = ProductInfoBLL.GetProductList(new ProductInfoSearch { StoreId = id });
                foreach (var item in ProductItemlist)
                {
                    XmlElement tagitem = xmldoc.CreateElement("item");
                    tagitem.SetAttribute("id", item.ID.ToString());
                    tagitem.SetAttribute("name", item.Name);
                    tagitem.SetAttribute("img", item.Bigimgname);
                    tagitem.SetAttribute("snapimg", item.Smallimgname);
                    tagitem.SetAttribute("desc", item.Description);
                    tagitem.SetAttribute("tag", item.Type.ToString());
                    ProductItemsElm.AppendChild(tagitem);
                }

                ProductElm.AppendChild(ProductTagsElm);
                ProductElm.AppendChild(ProductItemsElm);

                #endregion

                root.AppendChild(BarberElm);
                root.AppendChild(HairElm);
                root.AppendChild(ProductElm);
                SetStatusDateTime(id, FileStatus.Normal, DateTime.Now,null);//设置状态
                if (!Directory.Exists(UpFileUrl + "Shop_" + id ))
                {
                    Directory.CreateDirectory(UpFileUrl + "Shop_" + id);
                }
                xmldoc.Save(shoppath);
              
                //XmlWriter xmlstring=XmlWriter.Create("shopxml");
                //xmldoc.WriteContentTo(xmlstring);
                //return xmlstring.ToString();
            }
            catch { }
        }
        
    }
}
